package com.linkcard.list.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.list.manager.FeedbackManager;
import com.linkcard.list.model.Feedback;
import com.linkcard.single.model.SinglePage;
import com.linkcard.user.model.WebsiteColumns;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FeedbackAction extends ActionSupport {

	private FeedbackManager feedbackManager=null;
	private Feedback feedback=null;
	private Page pageObj=null;
	private int page;
	private int rows;
	private String template=null;
	private List list=null;
	@Override
	public String execute() throws Exception {
		List columnList=(List)ActionContext.getContext().getApplication().get("columns");
		for(int i=0;i<columnList.size();i++)
		{
			WebsiteColumns column=(WebsiteColumns)columnList.get(i);
			if(column.getId()==6)
			{
				template=column.getTemplate();
				break;
			}
		}
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(5);
		 list=this.feedbackManager.feedbackList(pageObj);
		return "success";
	}
	//添加留言
	public String addFeedback()throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		feedback.setContent(Helper.decode(feedback.getContent()));
		feedback.setTitle(Helper.decode(feedback.getTitle()));
		feedback.setUsername(Helper.decode(feedback.getUsername()));
		feedback.setDatetime(sdf.format(new Date()));
		boolean isSuccess=this.feedbackManager.addFeedback(feedback);
		List columnList=(List)ActionContext.getContext().getApplication().get("columns");
		for(int i=0;i<columnList.size();i++)
		{
			WebsiteColumns column=(WebsiteColumns)columnList.get(i);
			if(column.getId()==6)
			{
				template=column.getTemplate();
				break;
			}
		}
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(5);
		 list=this.feedbackManager.feedbackList(pageObj);
		return "success";
	}
	//获得留言列表
	public String feedbackListForJson()throws Exception{
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(rows);
		List list=this.feedbackManager.feedbackList(pageObj);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Feedback feedback=(Feedback)list.get(i);
			JSONObject temp=new JSONObject();
			temp.put("id", feedback.getId());
			temp.put("title", feedback.getTitle());
			temp.put("content",feedback.getContent());
			temp.put("username",feedback.getUsername());
			temp.put("tel", feedback.getTel());
			temp.put("email",feedback.getEmail());
			temp.put("datetime",feedback.getDatetime());
			temp.put("reply",feedback.getReply());
			temp.put("replyDatetime", feedback.getReplyDatetime());
			temp.put("opt", "<a href='javascript:void(0);' onclick='reply("+feedback.getId()+")'>回复</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteFeedback("+feedback.getId()+")'>删除</a>");
			jsonArray.add(temp);
		}
		json.put("rows",jsonArray);
		json.put("total", pageObj.getRowCount());
		backText(json.toString());
		return "Data";
	}
	//删除留言
	public String deleteFeedback()throws Exception {
		boolean isSuccess=this.feedbackManager.deleteFeedback(feedback.getId());
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//修改留言
	public String modifyFeedback()throws Exception{
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		feedback.setReplyDatetime(sdf.format(new Date()));
		feedback.setReply(Helper.decode(feedback.getReply()));
		boolean isSuccess=this.feedbackManager.modifyFeedback(feedback);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public FeedbackManager getFeedbackManager() {
		return feedbackManager;
	}
	public void setFeedbackManager(FeedbackManager feedbackManager) {
		this.feedbackManager = feedbackManager;
	}
	
	public Page getPageObj() {
		return pageObj;
	}
	public void setPageObj(Page pageObj) {
		this.pageObj = pageObj;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public Feedback getFeedback() {
		return feedback;
	}
	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}
	public String getTemplate() {
		return template;
	}
	public void setTemplate(String template) {
		this.template = template;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}

}
